package com.studyyoun.android.androidcommonbaselibrary.activity;

/**
 * Created by androidlongs on 17/7/14.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

import com.studyyoun.android.androidcommonbaselibrary.inter.OnBooleanListener;

/**
 *
 * CommonBaseActivity Base基类封装
 */

public abstract class CommonBaseActivity  extends AppCompatActivity{


    private OnBooleanListener onPermissionListener;
    protected Context mContext;
    protected Handler mHandler = new Handler(Looper.getMainLooper());
    public DisplayMetrics mDisplayMetrics;
    public float mScaledDensity;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this.getApplicationContext();
        setContentView(getCommonLayoutId());
        View view= View.inflate(this,getCommonLayoutId(),null);
        Intent intent = getIntent();
        getAllIntentExtraDatas(intent);
        commonInitView(view);
        commonFunction();

        mDisplayMetrics = mContext.getResources().getDisplayMetrics();
        mScaledDensity = mDisplayMetrics.scaledDensity;

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run () {
                commonDelayFunction ();
            }
        },200);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {

        }
    }

    //获取所有的传递数据
    protected abstract void getAllIntentExtraDatas (Intent intent);
    //获取布局ID
    protected abstract int getCommonLayoutId ();
    //初始化控件
    protected abstract void commonInitView (View view);
    //常用功能 处理
    protected abstract void commonFunction ();
    //延时常用功能 处理
    protected abstract void commonDelayFunction ();



    public void onPermissionRequests(String permission, OnBooleanListener onBooleanListener) {
        onPermissionListener = onBooleanListener;
        Log.d("MainActivity", "0");
        if (ContextCompat.checkSelfPermission(this,
                permission)
                != PackageManager.PERMISSION_GRANTED) {
            // Should we show an explanation?
            Log.d("MainActivity", "1");
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.READ_CONTACTS)) {
                //权限已有
                onPermissionListener.onClick(true);
            } else {
                //没有权限，申请一下
                ActivityCompat.requestPermissions(this,
                        new String[]{permission},
                        1);
            }
        }else{
            onPermissionListener.onClick(true);
            Log.d("MainActivity", "2"+ContextCompat.checkSelfPermission(this,
                    permission));
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 1) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //权限通过
                if (onPermissionListener != null) {
                    onPermissionListener.onClick(true);
                }
            } else {
                //权限拒绝
                if (onPermissionListener != null) {
                    onPermissionListener.onClick(false);
                }
            }
            return;
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}
